package com.nami.store.biz.privatehismsgs.service;

import cn.hutool.core.collection.CollUtil;
import com.nami.store.api.request.message.HisMsgQryRequest;
import com.nami.store.api.request.message.PrivateMsgReq;
import com.nami.store.api.response.message.MessageDTO;
import com.nami.store.biz.inboxmsgs.repository.InboxMsgsRepository;
import com.nami.store.biz.privatehismsgs.convert.PrivateMsgConvert;
import com.nami.store.biz.privatehismsgs.repository.PrivateHisMsgsRepository;
import com.nami.store.biz.sendboxmsgs.repository.SendboxMsgsRepository;
import com.nami.store.domain.InboxMsgsDO;
import com.nami.store.domain.PrivateHisMsgsDO;
import com.nami.store.domain.SendboxMsgsDO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class PrivateChatDomainServiceImpl implements PrivateChatDomainService {

    private final PrivateHisMsgsRepository privateHisMsgsRepository;

    private final InboxMsgsRepository inboxMsgsRepository;

    private final SendboxMsgsRepository sendboxMsgsRepository;

    @Override
    public boolean storePrivateMsg(PrivateMsgReq request) {

        SendboxMsgsDO sendboxMsgs = new SendboxMsgsDO();
        sendboxMsgs.setAppKey(request.getAppKey());
        sendboxMsgs.setMsgId(request.getMessageId());

        sendboxMsgs.setUserId(request.getSenderId());
        sendboxMsgs.setTargetId(request.getReceiverId());

        sendboxMsgs.setPayload(request.getPayload());
        sendboxMsgs.setPayloadType(request.getPayloadType());
        sendboxMsgs.setSendTime(request.getSendTime());
        sendboxMsgs.setConverType(1); // c2c
        sendboxMsgsRepository.save(sendboxMsgs);

        InboxMsgsDO inboxMsgs = new InboxMsgsDO();
        inboxMsgs.setMsgId(request.getMessageId());
        inboxMsgs.setUserId(request.getReceiverId());
        inboxMsgs.setTargetId(request.getSenderId());
        inboxMsgs.setAppKey(request.getAppKey());
        inboxMsgs.setPayload(request.getPayload());
        inboxMsgs.setPayloadType(request.getPayloadType());
        inboxMsgs.setSendTime(request.getSendTime());
        inboxMsgs.setConverType(1); // c2c
        inboxMsgsRepository.save(inboxMsgs);

        PrivateHisMsgsDO hisMsgs = new PrivateHisMsgsDO();
        hisMsgs.setMsgId(request.getMessageId());
        hisMsgs.setSenderId(request.getSenderId());
        hisMsgs.setReceiverId(request.getReceiverId());
        hisMsgs.setAppKey(request.getAppKey());
        hisMsgs.setPayload(request.getPayload());
        hisMsgs.setPayloadType(request.getPayloadType());
        hisMsgs.setSendTime(request.getSendTime());
        hisMsgs.setConverType(1); // c2c
        hisMsgs.setConverId(request.getCid());
        hisMsgs.setIfRead(Boolean.FALSE);

        hisMsgs.setClientMsgId(request.getClientMsgId());
        hisMsgs.setSeq(request.getSeq());

        hisMsgs.setCreateTime(new Date());

        privateHisMsgsRepository.save(hisMsgs);

        return Boolean.TRUE;
    }

    @Override
    public List<MessageDTO> getHisMsgs(HisMsgQryRequest request) {
        List<PrivateHisMsgsDO> hisMsgs = privateHisMsgsRepository.getHisMsgs(request);
        if (CollUtil.isEmpty(hisMsgs)) {
            return null;
        }
        return PrivateMsgConvert.INSTANCE.convertList(hisMsgs);
    }
}
